package com.yw.atp.model;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yw.atp.Constant;
import com.yw.atp.util.DateUtil;

@Entity
@Table(name=Constant.DB_PREFIX+"group")
public class Group implements IDEntity{
	private static final long serialVersionUID = 5804584232096736608L;
	@Id
    @GeneratedValue
	private Integer id;
	private String groupid;
	private String groupname;
	private String groupNameEn;
	private String groupDes;
	private String groupDesEn;
	private String orderId;
	private String status = "0";
	@Temporal(TemporalType.TIMESTAMP)
	private Date createdAt;
	@Temporal(TemporalType.TIMESTAMP)
	private Date updateAt;

	@OneToMany(mappedBy="groupid",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private Set<Apps> apps;
	public Set<Apps> getApps() {
		return apps;
	}
	public void setApps(Set<Apps> apps) {
		this.apps = apps;
	}
	public String getGroupid() {
		return groupid;
	}
	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}
	public String getGroupname() {
		return groupname;
	}
	public void setGroupname(String groupname) {
		this.groupname = groupname;
	}
	public String getGroupNameEn() {
		return groupNameEn;
	}
	public void setGroupNameEn(String groupNameEn) {
		this.groupNameEn = groupNameEn;
	}
	public String getGroupDes() {
		return groupDes;
	}
	public void setGroupDes(String groupDes) {
		this.groupDes = groupDes;
	}
	public String getGroupDesEn() {
		return groupDesEn;
	}
	public void setGroupDesEn(String groupDesEn) {
		this.groupDesEn = groupDesEn;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public Date getCreatedAt() {
		return createdAt;
	}
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	public Date getUpdateAt() {
		return updateAt;
	}
	public void setUpdateAt(Date updateAt) {
		this.updateAt = updateAt;
	}
	public static JSONArray capture(List<Group> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Group appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public JSONObject capture() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id", id);
		appm.put("groupid",groupid);
		appm.put("groupname",groupname);
		appm.put("groupNameEn",groupNameEn);
		appm.put("groupDes",groupDes);
		appm.put("groupDesEn",groupDesEn);
		appm.put("orderId",orderId);
		appm.put("createdAt",createdAt!=null?DateUtil.getServiceFormatTime(createdAt):" ");
		appm.put("updateAt",updateAt!=null?DateUtil.getServiceFormatTime(updateAt):" ");
		return appm;
	}
}
